package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.FarmHygieneExamineBirds;
import com.jiantong.lawEnforcing.service.FarmHygieneExamineBirdsService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 动物养殖场动物卫生监督检查情况表(奶牛)
 * Created by mz03 on 2017/9/29.
 */
@Slf4j
@OpenBean
public class FarmHygieneExamineBirdsApis {
    @RpcConsumer
    private FarmHygieneExamineBirdsService farmHygieneExamineBirdsService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.farmHygieneExamineBirds.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public FarmHygieneExamineBirds findById(Long id){
        Response<FarmHygieneExamineBirds> response = farmHygieneExamineBirdsService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find farmHygieneExamineBirds read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 创建一条信息
     * @param farmHygieneExamineBirds
     * @return
     */
    @OpenMethod(
            key = "create.farmHygieneExamineBirds",
            paramNames = {"farmHygieneExamineBirds"},
            httpMethods = RequestMethod.POST
    )
    public Long create(FarmHygieneExamineBirds farmHygieneExamineBirds){
        Response<Long> response = farmHygieneExamineBirdsService.create(farmHygieneExamineBirds);
        if(!response.isSuccess()){
            log.error("failed to create farmHygieneExamineBirds read by farmHygieneExamineBirds = {}, cause : {}", farmHygieneExamineBirds, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     * @param farmHygieneExamineBirds
     * @return
     */
    @OpenMethod(
            key = "update.farmHygieneExamineBirds",
            paramNames = {"farmHygieneExamineBirds"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(FarmHygieneExamineBirds farmHygieneExamineBirds){
        if(farmHygieneExamineBirds.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = farmHygieneExamineBirdsService.update(farmHygieneExamineBirds);
        if(!response.isSuccess()){
            log.error("failed to update farmHygieneExamineBirds");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(), "failed to update farmHygieneExamineBirds");
        }
        return response.getResult();
    }

    /**
     * 根据FarmHygieneExamineBirds对象 分页查询
     * @param pageNo
     * @param pageSize
     * @param farmHygieneExamineBirds FarmHygieneExamineBirds对象
     * @return
     */
    @OpenMethod(
            key = "find.paging.farmHygieneExamineBirds",
            paramNames =
                    {"pageNo","pageSize","farmHygieneExamineBirds"},
            httpMethods = RequestMethod.GET
    )
    public Paging<FarmHygieneExamineBirds> pagingCasesInformation(Integer pageNo, Integer pageSize,FarmHygieneExamineBirds farmHygieneExamineBirds){
        PageInfo pageInfo = new PageInfo(pageNo,pageSize);
        Response<Paging<FarmHygieneExamineBirds>> response = farmHygieneExamineBirdsService.paging(pageInfo.getOffset(),pageInfo.getLimit(),farmHygieneExamineBirds);
        if (!response.isSuccess()) {
            log.error("failed to find paging farmHygieneExamineBirds by farmHygieneExamineBirds = {}, cause : {}", farmHygieneExamineBirds, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

}
